﻿using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using System;

namespace docNET.Common
{
	internal class FileHandler
	{
		/// <summary>
		/// Prepares the save file dialog.
		/// </summary>
		/// <param name="s">The dialog to prepare.</param>
		/// <param name="fileType">File type to prepare for.</param>
		internal static void PrepareSaveFileDialog(ref SaveFileDialog s, FileType fileType)
		{
			switch (fileType)
			{
				case FileHandler.FileType.Project:
					s.AddExtension = true;
					s.DefaultExt = "dnproj";
					s.FileName = "";
					s.Filter = "docNET Project|*.dnproj";
					s.AutoUpgradeEnabled = true;
					var settings = docNET.Properties.Settings.Default;
					s.InitialDirectory = settings.DefaultProjectPath;
					break;
				case FileHandler.FileType.File:
					break;
				default:
					// TODO Throw exception
					break;
			}
		}

		/// <summary>
		/// Prepares the open file dialog.
		/// </summary>
		/// <param name="o">The dialog to prepare.</param>
		/// <param name="fileType">File type to prepare for.</param>
		internal static void PrepareOpenFileDialog(ref OpenFileDialog o, FileType fileType)
		{
			switch (fileType)
			{
				case FileHandler.FileType.Project:
					o.AddExtension = true;
					o.DefaultExt = "dnproj";
					o.FileName = string.Empty;
					o.Filter = "docNET Project|*.dnproj";
					o.AutoUpgradeEnabled = true;
					var settings = docNET.Properties.Settings.Default;
					o.InitialDirectory = settings.DefaultProjectPath;
					break;
				case FileHandler.FileType.File:
					break;
				default:
					// TODO Throw exception
					break;
			}
		}

		/// <summary>
		/// Saves current project.
		/// </summary>
		internal static void SaveProject()
		{
			if (Current.project == null)
				return;

			// Create temporary filename and save it for restore if crash.
			var tempFileName = string.Format("{0}~{1}.dnproj", Current.project.ProjectFilesFolder.Substring(0, Current.project.ProjectFilesFolder.Length - Current.project.Name.Length), Current.project.Name);
			var settings = docNET.Properties.Settings.Default;
			settings.TempProjectFile = tempFileName;
			settings.Save();

			// Create the project files folder if needed.
			if (!Directory.Exists(Current.project.ProjectFilesFolder))
				Directory.CreateDirectory(Current.project.ProjectFilesFolder);

			// Serialize the current project and write it to the project file.
			Current.project.LastSaved = DateTime.Now;
			XmlSerializer s = new XmlSerializer(Current.project.GetType());
			StreamWriter w = new StreamWriter(tempFileName);
			s.Serialize(w, Current.project);
			w.Flush();
			w.Close();

			// Overwrite the project file with the temporary file.
			if(File.Exists(Current.project.ProjectFile))
				File.Delete(Current.project.ProjectFile);

			File.Move(tempFileName, Current.project.ProjectFile);
		}

		/// <summary>
		/// Opens a project.
		/// </summary>
		/// <param name="projectFileName">Project file to open.</param>
		internal static void OpenProject(string projectFileName)
		{
			XmlSerializer s = new XmlSerializer(typeof(Project));
			StreamReader r = new StreamReader(projectFileName);
			Current.project = (Project)s.Deserialize(r);
			r.Close();
		}

		/// <summary>
		/// Specifies file types for file dialogs.
		/// </summary>
		internal enum FileType
		{
			Project,
			File
		}
	}
}
